package com.hbt.en_rem_hou.service;

import com.hbt.en_rem_hou.dao.UserDao;
import com.hbt.en_rem_hou.dao.WordDao;
import com.hbt.en_rem_hou.entitys.StaticBean;
import com.hbt.en_rem_hou.entitys.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImp implements UserService{
    @Autowired
    UserDao dao;

    @Autowired
    WordDao wordDao;

    @Override
    public boolean register(User user) {
        if(dao.insertUser(user)>0){
            return true;
        }
        return false;
    }

    @Override
    public StaticBean getProgress(int userid) {
        //获得总单词数totalCount
        int total = wordDao.getTotalCountByUserId(userid);
        //分别获得三个熟练度和三个重要度的count
        int henCmd = wordDao.getHenCmdCountByUserId(userid)*100/total;
        int cmd = wordDao.getCmdCountByUserId(userid)*100/total;
        int noCmd = wordDao.getNoCmdCountByUserId(userid)*100/total;
        int henImp = wordDao.getHenImpCountByUserID(userid)*100/total;
        int imp = wordDao.getImpCountByUserID(userid)*100/total;
        int noImp = wordDao.getNoImpCountByUserID(userid)*100/total;

        StaticBean staticBean = new StaticBean(total, "total");
        staticBean.getStaticBeans().add(new StaticBean(henCmd, "很熟练"));
        staticBean.getStaticBeans().add(new StaticBean(cmd, "熟练"));
        staticBean.getStaticBeans().add(new StaticBean(noCmd, "不熟练"));
        staticBean.getStaticBeans().add(new StaticBean(henImp, "很重要"));
        staticBean.getStaticBeans().add(new StaticBean(imp, "重要"));
        staticBean.getStaticBeans().add(new StaticBean(noImp, "不重要"));
        return staticBean;
    }

    @Override
    public int login(User user) {
        User user_get = dao.getUserByUsernameAndPassword(user);
        if(user_get!=null){
            System.out.println("登录成功:"+user_get);
            return user_get.getId();
        }
        return 0;
    }

    public User getUserInfoById(int id){
        return dao.getUserByID(id);
    }
}
